home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / mpeg_play-2.1 / motionvector.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  6KB  |  207 lines

  1. /*
  2.  * motionvector.c --
  3.  *
  4.  *       Procedures to compute motion vectors.
  5.  *
  6.  */
  7.  
  8. /*
  9.  * Copyright (c) 1995 The Regents of the University of California.
  10.  * All rights reserved.
  11.  * 
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose, without fee, and without written agreement is
  14.  * hereby granted, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  * 
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  * 
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. #include "video.h"
  30. #include "proto.h"
  31. #include "util.h"
  32.  
  33.  
  34. /*
  35.  *--------------------------------------------------------------
  36.  *
  37.  * ComputeVector --
  38.  *
  39.  *    Computes motion vector given parameters previously parsed
  40.  *      and reconstructed.
  41.  *
  42.  * Results:
  43.  *      Reconstructed motion vector info is put into recon_* parameters
  44.  *      passed to this function. Also updated previous motion vector
  45.  *      information.
  46.  *
  47.  * Side effects:
  48.  *      None.
  49.  *
  50.  *--------------------------------------------------------------
  51.  */
  52.  
  53. #define ComputeVector(recon_right_ptr, recon_down_ptr, recon_right_prev, recon_down_prev, f, full_pel_vector, motion_h_code, motion_v_code, motion_h_r, motion_v_r)                \
  54.                                     \
  55. {                                    \
  56.   int comp_h_r, comp_v_r;                        \
  57.   int right_little, right_big, down_little, down_big;            \
  58.   int max, min, new_vector;                        \
  59.                                     \
  60.   /* The following procedure for the reconstruction of motion vectors     \
  61.      is a direct and simple implementation of the instructions given    \
  62.      in the mpeg December 1991 standard draft.                 \
  63.   */                                    \
  64.                                     \
  65.   if (f == 1 || motion_h_code == 0)                    \
  66.     comp_h_r = 0;                            \
  67.   else                                     \
  68.     comp_h_r = f - 1 - motion_h_r;                    \
  69.                                     \
  70.   if (f == 1 || motion_v_code == 0)                    \
  71.     comp_v_r = 0;                            \
  72.   else                                     \
  73.     comp_v_r = f - 1 - motion_v_r;                    \
  74.                                     \
  75.   right_little = motion_h_code * f;                    \
  76.   if (right_little == 0)                        \
  77.     right_big = 0;                            \
  78.   else {                                \
  79.     if (right_little > 0) {                        \
  80.       right_little = right_little - comp_h_r;                \
  81.       right_big = right_little - 32 * f;                \
  82.     }                                    \
  83.     else {                                \
  84.       right_little = right_little + comp_h_r;                \
  85.       right_big = right_little + 32 * f;                \
  86.     }                                    \
  87.   }                                    \
  88.                                     \
  89.   down_little = motion_v_code * f;                    \
  90.   if (down_little == 0)                            \
  91.     down_big = 0;                            \
  92.   else {                                \
  93.     if (down_little > 0) {                        \
  94.       down_little = down_little - comp_v_r;                \
  95.       down_big = down_little - 32 * f;                    \
  96.     }                                    \
  97.     else {                                \
  98.       down_little = down_little + comp_v_r;                \
  99.       down_big = down_little + 32 * f;                    \
  100.     }                                    \
  101.   }                                    \
  102.                                       \
  103.   max = 16 * f - 1;                            \
  104.   min = -16 * f;                            \
  105.                                     \
  106.   new_vector = recon_right_prev + right_little;                \
  107.                                     \
  108.   if (new_vector <= max && new_vector >= min)                \
  109.     *recon_right_ptr = recon_right_prev + right_little;            \
  110.                       /* just new_vector */                \
  111.   else                                    \
  112.     *recon_right_ptr = recon_right_prev + right_big;            \
  113.   recon_right_prev = *recon_right_ptr;                    \
  114.   if (full_pel_vector)                            \
  115.     *recon_right_ptr = *recon_right_ptr << 1;                \
  116.                                     \
  117.   new_vector = recon_down_prev + down_little;                \
  118.   if (new_vector <= max && new_vector >= min)                \
  119.     *recon_down_ptr = recon_down_prev + down_little;            \
  120.                       /* just new_vector */                \
  121.   else                                    \
  122.     *recon_down_ptr = recon_down_prev + down_big;            \
  123.   recon_down_prev = *recon_down_ptr;                    \
  124.   if (full_pel_vector)                            \
  125.     *recon_down_ptr = *recon_down_ptr << 1;                \
  126. }
  127.  
  128.  
  129. /*
  130.  *--------------------------------------------------------------
  131.  *
  132.  * ComputeForwVector --
  133.  *
  134.  *    Computes forward motion vector by calling ComputeVector
  135.  *      with appropriate parameters.
  136.  *
  137.  * Results:
  138.  *    Reconstructed motion vector placed in recon_right_for_ptr and
  139.  *      recon_down_for_ptr.
  140.  *
  141.  * Side effects:
  142.  *      None.
  143.  *
  144.  *--------------------------------------------------------------
  145.  */
  146.  
  147. void 
  148. ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr)
  149.      int *recon_right_for_ptr;
  150.      int *recon_down_for_ptr;
  151. {
  152.  
  153.   Pict *picture;
  154.   Macroblock *mblock;
  155.  
  156.   picture = &(curVidStream->picture);
  157.   mblock = &(curVidStream->mblock);
  158.  
  159.   ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
  160.         mblock->recon_right_for_prev, 
  161.         mblock->recon_down_for_prev,
  162.         (int) picture->forw_f,
  163.         picture->full_pel_forw_vector,
  164.         mblock->motion_h_forw_code, mblock->motion_v_forw_code,
  165.         mblock->motion_h_forw_r, mblock->motion_v_forw_r); 
  166. }
  167.  
  168.  
  169. /*
  170.  *--------------------------------------------------------------
  171.  *
  172.  * ComputeBackVector --
  173.  *
  174.  *    Computes backward motion vector by calling ComputeVector
  175.  *      with appropriate parameters.
  176.  *
  177.  * Results:
  178.  *    Reconstructed motion vector placed in recon_right_back_ptr and
  179.  *      recon_down_back_ptr.
  180.  *
  181.  * Side effects:
  182.  *      None.
  183.  *
  184.  *--------------------------------------------------------------
  185.  */
  186.  
  187. void 
  188. ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr)
  189.      int *recon_right_back_ptr;
  190.      int *recon_down_back_ptr;
  191. {
  192.   Pict *picture;
  193.   Macroblock *mblock;
  194.  
  195.   picture = &(curVidStream->picture);
  196.   mblock = &(curVidStream->mblock);
  197.  
  198.   ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
  199.         mblock->recon_right_back_prev, 
  200.         mblock->recon_down_back_prev,
  201.         (int) picture->back_f, 
  202.         picture->full_pel_back_vector,
  203.         mblock->motion_h_back_code, mblock->motion_v_back_code,
  204.         mblock->motion_h_back_r, mblock->motion_v_back_r); 
  205.  
  206. }
  207.